\f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \u160 \line \line \line \line \line \line \line \line \line \line By:Alex Sink (HardCoded\u8224?)\line Edited By: Cerebus and ZiffDuck\line \line \line \line In this article I hope to cover the basics of the basics, ANSI C. However, you must keep in mind that this is just an intro to C, and you may not completely understand what I say at first, but as you read on, things will make more since.\line \line \line First let's get the name straight, for you beginners. The ANSI (Accredited Standards Committee) standard is an attempt to make C code portable (movable across many platforms, so that you only have to recompile the same code, instead of having to completely re-write the program). C evolved from a language called B (actually BCPI-a split off of B), so it seemed natural to call it C.\line \line Also, when you write C programs, your computer does not understand them. You must have a compiler to translate your program into machine code. Once it has been translated to machine code, it can be run like any other program, and understood by your computer.\line \line \line \f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 Usefulness of C: \f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line \line C is used for EVERYTHING. C is a very powerful language that is used for things ranging from microcontroller programming to CGI programming. Also, if you are a C++ programmer you must understand the style and ways of C; because almost all existing header files and external libraries are programmed in C. C is very efficient, giving a ratio of 2.5 Assembly (a computer language that works directly with hardware) commands for every one C command (compared to up to 25 in some languages!). \line \line \line C programmers crave efficiency and love finding new ways to do things. C is a useful language that all programmers must know, and almost all other languages are made so that C programmers will have an easy transition to the other language. It is still the basis for one of the main Computer Science classes; which will soon change over to C++,which is not too hard if you know C already. And best of all, if you are into computer security, almost all of your exploits and patches are written in C, so it's a good thing to understand it and be able to use it.\line \line \line \line \line \line \line \f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 ON TO THE MEAT:\f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line \line First, you must have an ANSI compliant C (or C++) compiler and most likely an ascii text editor. All you have to do is plug in some C code and your compiler, when executed and pointed to your file, will turn it into machine-readable code, and your program will be made.\line \line -make the file hello.c with the contents:\line \line \f0\b0\i0\outl0\shad0\fs20 \cf1 \expan0 \ul0 /* Yay! It's my first program! This is a comment inside your code */\line #include <stdio.h>\line main()\line {\line \line printf("Hello World!\\n");\line \line return 0;\line }\line \f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line -put this file into your compiler and your done with your first program\line \line \line Line by line and from top to bottom (the way the code is read by your computer):\line \f0\b0\i0\outl0\shad0\fs20 \cf1 \expan0 \ul0 \line \line \line \line /* Yay! It's my first program! This is a comment inside your code */\line \f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 This is a comment. It is used so you can read, in plain English -or any other language for that matter, what you tried to write the night before. Whatever is between /* -blah- */ is ignored by compiler (computer) and is only used for making the code easy to understand.\line \line \line \line \f0\b0\i0\outl0\shad0\fs20 \cf1 \expan0 \ul0 \line #include <stdio.h>\f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line Basically this tells your compiler that you want everything in stdio.h to be added to your file. The '#include' statement tells your compiler to add whatever code is in <*.h> (header files containing preexisting code) or <*.c> to your file. stdio.h is a file that contains needed information to your basic program. \line \line \line \f0\b0\i0\outl0\shad0\fs20 \cf1 \expan0 \ul0 main()\f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line This is the only thing that is always read in your program. "main()" holds your whole program. This is the heart and soul of your program, experienced programmers try to keep it short and sweet using functions.\line \line \line \line \f0\b0\i0\outl0\shad0\fs20 \cf1 \expan0 \ul0 \line {\f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line This tells your program to group everything together after it as one thing. And because main() technically only reads one thing, this statement is always necessary.\line \line \line \line \line \line \f0\b0\i0\outl0\shad0\fs20 \cf1 \expan0 \ul0 printf("Hello World!\\n");\f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line This is a Function call to printf(). A function contains a group of code, so that your main() statement can be easily managed, so you can use it without worrying about how it works (term-encapsulation- when you do not have to worry about the implementation details of a function to be able to use it). Anything inside the parentheses,"()"s, is printed directly to the screen. SO this bit of code prints "Hello World!" because that is what is inside the parentheses. printf("HardCoded is cool") would print -HardCoded is cool- (without the dashes of course). The '\\n' character simply makes a new line, so the output doesn't look so messy.\line \line \line The little ";" at the end tells your program that this is a single, complete statement. If you do not put a semi-colon after a statement(any sort of command) it will cause your compiler to produce errors. Your computer is dumb, so it would read:\line (an example)\line \f0\b0\i0\outl0\shad0\fs20 \cf2 \expan0 \ul0 printf("Hello World! \\n")\line return 0;\f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line As ONE complete statement, even though it's not. This is a common error, so watch out for it. It gets very easy to leave out a semi-colon and have to re-compile your whole program because of it... which becomes a pain in the rear really quick, especially since compiling large programs can take a long time.\line \line \line --->\f0\b0\i0\outl0\shad0\fs20 \cf1 \expan0 \ul0 return 0;\f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line Basically this tells your computer everything closed with no problems, if it gets this far, that is. This tells the main() function (yes it's a function) to represent the value 0 (essentially) which, when read by your OS, telling it that it exited normally and that everything is OK. This statement exits the main() function, so always put it last.\line \line \line --->\f0\b0\i0\outl0\shad0\fs20 \cf1 \expan0 \ul0 }\f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line This ends the BLOCK STATEMENT that the "{" started. Everything between the { and } operators (built-in functions) is treated as one statement by the function that contains it(in this case, the main() function.\line \line There's your first program.\line \line The output is very simple:"Hello World", but you are well on your way to living your whole life in a basement, where all you want to do is "add that new feature". ;-P\line \line \line ------------------------------------------------------------\line \line \line \line Alrighty then. It's time to learn more fun stuff. Your second program will ask your users name, and then tell them to have a nice day. Be careful, this is a little complex. Remember that the best way to learn, is by messing with the code yourself, and soon you will understand how to use it.\line \line ------------------starting code:\line \f0\b0\i0\outl0\shad0\fs20 \cf1 \expan0 \ul0 /*\line most programmers like to say what the program is about at the top so they will know what the program does without having to look through all of the source.\line \line Greeter program-->written by HardCoded\line asks for someone's name, and tells them to have a nice day.\line 11/3/98 last change date\line */\line \line #include <stdio.h>\line \line int main(void)\line \line {\line \line char tempchar;\line \line printf("What is your name?");\line \line scanf("%c", &tempchar);\line \line while(tempchar != '\\n')\line \line \line {\line \line \line \line printf("%c", tempchar);\line \line \line \line tempchar = ' ';\line \line \line \line scanf("%c", &tempchar);\line \line \line }\line \line printf(", have a nice day.");\line \line return 0;\line \line }\line \f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line ---------------------------\line \line \line \line \line \line \line \line \line \line \line \line \line \line \line \f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 Output\f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line "What is your name?" (I type)->Alex\line (returned) "Alex, have a nice day."\line \line \line \line \line \line \line \line \line \line \line \line \line \line \line \line \f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 Analysis\line \f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line Now that didn't hurt, now did it? It has some new terms(isn't pretty much everything a new term to you?), but it's cool.\line \line \f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 Line By Line:\f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line \f0\b0\i0\outl0\shad0\fs20 \cf1 \expan0 \ul0 \line \line \line /*\line \line \line most programmers like to say what the program is about at the top\line \line \line so they can understand what the program does without having to look at all of the code.\line \line \line Greeter program-->written by HardCoded \line \line \line asks for someone's name, and tells them to have a nice day.\line \line \line 11/3/98 last change date\line \line \line \line */\line \f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line \line \line \line \line I consider this one line, because it acts as a single comment. Everything within the /* */ marks are ignored. These comments are used throughout code to help clarify the code, tell you what the code assumes(in big projects), and tell you anything else (maybe a joke, if it's a really long program).\line \line \line \line \line \f0\b0\i0\outl0\shad0\fs20 \cf1 \expan0 \ul0 int main(void)\f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line \line \line \line \line \line This is declaring your main() function. This tells your computer (compiler) to look here. Main() is the only function that is always read. Int is the variable returned to your OS, and void is what is going into the function, but I will cover that in more detail when I do functions.\line \line \line \line \line \f0\b0\i0\outl0\shad0\fs20 \cf1 \expan0 \ul0 {\f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line \line \line \line \line \line This tells your compiler (computer) to start the main() function and that everything inside of the brackets is a part of main(), until it hits an ending bracket. The brackets tell your compiler to look at everything between this operator, and the closing bracket operator(that matches with this one) as one line of code. So everything between them is treated as one line of code. Declaring functions without using brackets is useful if you are a lazy coder, or making a really small project. When making large projects, brackets make reading your code quicker, and easier. "Time is money"\line \line \line \line \line \f0\b0\i0\outl0\shad0\fs20 \cf1 \expan0 \ul0 char tempchar;\f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line \line \line \line \line \line Something interesting! This is declaring a variable! YAY! Something that you can manipulate, and make whatever you want... well almost. This particular type of variable is of type char. This means that it only holds characters. If you want to use text, you use this type of variable. The variable name follows the type of variable so the syntax would be: \line \line \line \f0\b0\i0\outl0\shad0\fs20 \cf2 \expan0 \ul0 char <name of char>; \f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line \line \line "tempchar" is the name of the variable.... for reasons that will become apparent. You name these guys whatever you want, but be careful not to name them a or b- stuff like that, unless you only use them for one small temporary job. It's best to name them whatever they are holding. Also, the name of the variable can't have spaces.\line \line \line \f0\b0\i0\outl0\shad0\fs20 \cf1 \expan0 \ul0 \line printf("What is your name?");\f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line \line \line \line printf() once again is a basic print function printf stands for print formatted.\line \line \line \line \f0\b0\i0\outl0\shad0\fs20 \cf1 \expan0 \ul0 scanf("%c", &tempchar);\f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line \line \line alright... take this one at a time:\line \line \line Scanf() - This searches the keyboard. It takes input, basically. What they type in gets put inside the variable that is inside the parentheses. So it works like this. \line \line \line \line \line \line \line \f0\b0\i0\outl0\shad0\fs20 \cf2 \expan0 \ul0 scanf( &varname ); \f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line But the scary thing is that it doesn't actually read the characters until the user his the <enter> key, which is represented by the "\\n" or newline character. So when you hit enter, you are really sending a newline, \\n, character to your computer, and telling it to read what you just wrote. In summary, if I type 5 and then enter one I am sending 5 and \\n to my computer and then the variable takes on the character value of 5, not the numerical value.... but that's a different mess.\line \line "%c", - what kind of variable are we searching for? %c or character (numbers are originally inputted as characters too!) variable. The %c then puts the data into the first variable (since it is the first control character) after the comma, which happens to be tempchar.\line \line "&tempchar"- alright... so what is that & thingy doing? It's the reference operator... but for now, all you need to know is that you have to use it when using scanf(),or else your program will have some major problems, and it won't work.\line \line \line \f0\b0\i0\outl0\shad0\fs20 \cf1 \expan0 \ul0 while(tempchar != '/n')\f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line \line \line Your program normally reads things from top to bottom, and then quits. This kind of Control Statement is what changes that. \line \line while() - the statement block(what's in the {}'s) of the while statement gets executed when what's in the parentheses is evaluated as true, or as anything but zero. So if you put while(1), then the statement block of the while statement will keep on executing over and over again.\line \line \line (tempchar != '/n') - what the heck am I trying to say by this? Tempchar is the value that I just scanned in using scanf(). So, if your victim typed in Suzie, my first character would be S. The -- != '/n' -- part, translated means is not assigned to /n(<enter>). \line \line \line The " ! " means not. \line \line \line The "=" means assigned to (different than 'equals to'), but when used with another operator like this, it evaluates whether it is true or false and returns a one(true) or a zero(false). \line \line \line '/n' means, the character "/n" you use single quotes to indicate an individual character. So, while tempchar is not enter, it will keep executing. \line \line \line In my Suzie example, it would say no, S is not a newline(/n) character, so it evaluates as true(non-zero). The statement that the first character would make is while(1), and that executes the code inside the statement block(the stuff between the { } 's ). Next is 'u',then 'z', then 'i', and 'e' these would react the same way as an S, for they are not '/n' characters. They would all run the code in the statement block (which prints the character in tempchar) \line \line \line Then... suddenly at the end of Suzie there is a /n character... so what was actually typed was Suzie/n(the /n counts as one character). When it sees the /n character, it says,"no way! I know you are a /n character... you are a ZERO!!! You are false!" and skips what is inside the statement block and move on to the next statement which is the printf(", have a nice day"); statement. --What I'm trying to say in plain English- this accepts everything that you type in the keyboard, but when you hit the enter (newline) key, you make it false, and it moves on.\line \line \line \f0\b0\i0\outl0\shad0\fs20 \cf1 \expan0 \ul0 \line \line {\f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line \line \line \line This marks the beginning of the while() statement block. Whatever is inside of it is part of the while() statement block.\line \line \line \line \line \line \f0\b0\i0\outl0\shad0\fs20 \cf1 \expan0 \ul0 printf("%c", tempchar);\f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line \line \line Something interesting about this... why isn't the tempchar inside the quotes? Because if it was it would just print, literally tempchar. Your computer is stupid, so it is you who has to be smart. Taking it apart...\line \line \line printf()-you already know this... it's the same thing as before, you just put different stuff in it.\line \line \line "%c", - Holy moley! What does that mean? just tells your computer to look for the character variable and print it's value. %c tells it to look for a character. You can use %d to look for a variable type operator... etc... I'll go over those as we come to them. You use the comma to separate the parentheses and the variable, once again because your computer is stupid and needs a signal for where to look for the variable.\line \line \line tempchar - this variable is read from the "%c" statement. Whatever it's value is, is printed.\line \line \line \line \line \line \f0\b0\i0\outl0\shad0\fs20 \cf1 \expan0 \ul0 scanf(&tempchar);\f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line \line \line Just using the scanf() statement to read the next character, because tempchar(because it is just a normal char and not a string) can only hold one character. Strings make your life easier when doing this sort of thing, but this method is more simple, and can be useful at times. Everything has to be read in one character at a time, and then printed because of this limitation. However, this is a good technique to learn.\line \f0\b0\i0\outl0\shad0\fs20 \cf1 \expan0 \ul0 \line \line \line \line }\line \f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line ----> this ends the while() statement block.\line \line \line \f0\b0\i0\outl0\shad0\fs20 \cf1 \expan0 \ul0 \line printf(", have a nice day.");\f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line \line \line Good ole familiar printf()... so by now the name of the person running the program should have been printed by the constant scanning and printing of the while( tempchar != '\\n') loop (statement)... but it will not add a space... once again your computer is dumb, but it appears to be brilliant only because of the genius of it's programmers. (ego is good ;-). Now that the name has been scanned in letter by letter, and printed letter by letter, you now print your message: Whoever, have a nice day. Just a little toy, try messing around with the source, and have some fun with it!\line \line \f0\b0\i0\outl0\shad0\fs20 \cf1 \expan0 \ul0 \line return 0;\f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line \line \line If your computer gets to this statement, everything is OK!!!!! It quits successfully and everyone is happy.\line \line \f0\b0\i0\outl0\shad0\fs20 \cf1 \expan0 \ul0 \line }\line \f0\b0\i0\outl0\shad0\fs20 \cf0 \expan0 \ul0 \line \line \line \line It's time to end this program and this article.\line \line \line This is just a basic intro to intro C. Keep reading for more on C!}